﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sys_rolemange : System.Web.UI.Page
{
    PM.PMSys.authority auth = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView(true);
        }
    }
    //分页触发事件
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView(false);
    }

    protected void BindGridView(bool GetCount)
    {
        PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "Sys_UserType", "id", "id,typename,typeid,rating,state", "id asc", "1=1",GetCount);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //实例化权限类
        auth = new PM.PMSys.authority();
        int id = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);

        if (e.CommandName == "deleterole")
        {
            if (auth.Delete_Authority(id) > 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "已删除此角色！");
                BindGridView(false);
            }
        }
    }
    //结束编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindGridView(false);
    }
    //行编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView(false);
    }
    //行更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //实例化权限类
        auth = new PM.PMSys.authority();
        //获取行主键值
        int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value);
        //获取修改值
        string rolename =((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
        string roleid = GridView1.Rows[e.RowIndex].Cells[2].Text.Trim();
        bool status = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Checked;

        if (rolename == string.Empty || roleid == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "数据不能为空");
            return;
        }
        if (roleid.Length > 2)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "角色编号不能大于两位！");
            return;
        }
        if (auth.Update_Authority(rolename, roleid, id,status)>0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "更新成功！");
            this.GridView1.EditIndex = -1;
            BindGridView(false);
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[2].Text.Trim() != string.Empty)
            {
                string usertype = e.Row.Cells[2].Text.Trim();
                if (usertype == "1" || usertype == "2" || usertype == "3" || usertype == "4")
                {
                    ImageButton ib = (ImageButton)e.Row.FindControl("ImageButton1");
                    e.Row.Cells[4].Enabled = false;
                    ib.Enabled = false;
                    ib.ToolTip = "系统角色不能删除！";
                }
            }
        }
    }
}
